using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Commons;
using WPFBPMN.Utils;

namespace CustomControls
{
    public class BPMNLane : BPMNContainer
    {
        public override Brush Background
        {
            get
            {
                return DrawingCanvas.Background;
            }
            set
            {
                DrawingCanvas.Background = value;
            }
        }
        public BPMNLane(BPMNShape parent, List<string> additionalArguments)
            : base(parent)
        {
            BPMNShapeType = ShapeType.BPMNLane;
            CanBeUnlocked = false;
            LockMoving = true;
            DrawingCanvas.Background = Brushes.White;
        }
        public override void MoveWithReposition(BPMNShape drawing)
        {
            double _left = Mouse.GetPosition(this).X - drawing.Offset.X;
            double _top = Mouse.GetPosition(this).Y - drawing.Offset.Y;
            double maxLeft = Width - drawing.Width - Title.Height;
            double maxTop = Height - drawing.Height;
            if (_left < 0)
            {
                _left = 0;
            }
            if (_top < 0)
            {
                _top = 0;
            }
            if (_left > maxLeft)
            {
                _left = maxLeft;
            }
            if (_top > maxTop)
            {
                _top = maxTop;
            }
            drawing.MoveTo(_left, _top);
            drawing.UpdateConnectingPoints();
            drawing.UpdateConnectedObjects();
        }

        public override void BPMNInitialize()
        {
            base.BPMNInitialize();
            Width = (ShapeParent as BPMNContainer).DrawingCanvas.Width;
            Height = (ShapeParent as BPMNContainer).DrawingCanvas.Height;
            Canvas.SetTop(this, 0);
            Canvas.SetLeft(this, 0);
        }
        public override void ArangeChildren()
        {
            if (ShapeParent != null && ShapeParent.Children.Count > 0)
            {
                Canvas.SetTop(ShapeParent.Children[0], 0);
                BPMNShape previousShape = null;
                double height = 0;
                for (int i = 1; i < ShapeParent.Children.Count; i++)
                {
                    previousShape = ShapeParent.Children[i - 1] as BPMNShape;
                    height += previousShape.Height;
                    Canvas.SetTop(ShapeParent.Children[i], Canvas.GetTop(previousShape) + previousShape.Height);
                    previousShape.Width = (ShapeParent as BPMNContainer).DrawingCanvas.Width;
                }
                if (height != 0)
                {
                    ShapeParent.Height = height;
                }
                previousShape = ShapeParent.Children[ShapeParent.Children.Count - 1] as BPMNShape;
                previousShape.Width = (ShapeParent as BPMNContainer).DrawingCanvas.Width;
            }
        }
    }
}
